package org.jeecg.modules.quartz.job;

import java.util.Collection;
import java.util.Iterator;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import io.finer.erp.jeecg.bas.entity.ZrkOrderProcess;
import io.finer.erp.jeecg.bas.entity.ZrkShoporder;
import io.finer.erp.jeecg.bas.entity.ZzShoporder;
import io.finer.erp.jeecg.bas.service.IZrkOrderProcessService;
import io.finer.erp.jeecg.bas.service.IZrkShoporderService;
import io.finer.erp.jeecg.bas.service.IZzShoporderService;

/**
 * 
* @ClassName: ShopOrderStatusJob 
* @Description: 监控生产工单各工序完成情况，如果发现某工单下所有工序都已经完成则修改主任务单状态为已完成 
* @author heckerstone heckerstone@163.com
* @date Sep 2, 2020 5:52:53 PM 
*
 */
public class ShopOrderStatusJob implements Job {
	@Autowired
	private IZzShoporderService zzShoporderService;
	@Autowired
	private IZrkOrderProcessService zrkOrderProcessService;
	@Autowired
	private IZrkShoporderService zrkShoporderService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
         //查询所有主工单信息
		Collection<ZzShoporder> collection=zzShoporderService.list();
		if(null!=collection&&collection.size()>0) {
			Iterator<ZzShoporder> iterator=collection.iterator();
			while(iterator.hasNext()) {
				ZzShoporder zzShoporder=iterator.next();
				String flag="";//标志主订单下所有工序是否全部完成
				//查询主订单下所有工序信息
				Collection<ZrkOrderProcess> opCol=zrkOrderProcessService.list();
				if(null!=opCol&&opCol.size()>0) {
					Iterator<ZrkOrderProcess> zopIte=opCol.iterator();
					while(zopIte.hasNext()) {
						ZrkOrderProcess zrkOrderProcess=zopIte.next();
						if(zrkOrderProcess.getIsFinish()==1) {
							flag+="1";//统计完成
						}else {
							flag+="0";//统计未完成
						}
					}
				}
				//未找到未完成的记录
				if(flag.indexOf("0")<0) {
					//主订单设置未已完成
					zzShoporder.setWoStatus(4);//主订单状态置为4，订单完成
					zzShoporderService.updateById(zzShoporder);
					//更新销售订单状态为生产完成
					ZrkShoporder zrkShoporder=zrkShoporderService.getById(zzShoporder.getOrderNo());
					zrkShoporder.setOrderStatus("9");
					zrkShoporderService.updateById(zrkShoporder);//更新销售订单状态
				}
			}
		}
	}

}
